package com.hebeiwangxiaozhang.product.service;

import com.hebeiwangxiaozhang.product.pojo.ShoppingCarts;
import com.hebeiwangxiaozhang.product.pojo.ShoppingCartsSearchResults;
import com.hebeiwangxiaozhang.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ShoppingCartsService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    public Object addOrUpdate(ShoppingCarts shoppingCarts) {
        shoppingCarts.setId(shoppingCarts.getPersonId().concat("_").concat(shoppingCarts.getSkuId().toString()));
        return mongoTemplate.save(shoppingCarts, "carts");
    }

    public Object delete(ShoppingCarts shoppingCarts) {
        //进行删除，其实是需要和 mogodb进行一次沟通，沟通就会有网络开销，我们既然要删除数据，何不传关键的字段呢？
//        shoppingCarts.setId(shoppingCarts.getPersonId().concat("_").concat(shoppingCarts.getSkuId().toString()));
//        return mongoTemplate.remove(shoppingCarts, "carts");
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(shoppingCarts.getPersonId().concat("_").concat(shoppingCarts.getSkuId().toString())));
        return mongoTemplate.remove(query, "carts");
    }

    public List<ShoppingCartsSearchResults> search(String personId) {
        Aggregation aggregation = Aggregation.newAggregation(
            Aggregation.match(Criteria.where("personId").is(personId)),
            Aggregation.group("shopId")
                .first("shopId").as("shopId")
                .push("$$ROOT").as("carts")
        );
        AggregationResults<ShoppingCartsSearchResults> carts
                = mongoTemplate.aggregate(aggregation, "carts", ShoppingCartsSearchResults.class);
        return carts.getMappedResults();
    }

    public ShoppingCarts addOrUpdateRedis(ShoppingCarts shoppingCarts) {
        BoundHashOperations<String, Object, Object> boundHashOperations
                = redisTemplate.boundHashOps(shoppingCarts.getPersonId());
        boundHashOperations.put(shoppingCarts.getSkuId().toString(), shoppingCarts);
        return shoppingCarts;
    }

    public Object deleteRedis(ShoppingCarts shoppingCarts) {
        BoundHashOperations<String, Object, Object> boundHashOperations
                = redisTemplate.boundHashOps(shoppingCarts.getPersonId());
        boundHashOperations.delete(shoppingCarts.getSkuId().toString());
        return null;
    }

    public List<ShoppingCartsSearchResults> searchRedis(String personId) {
        List<ShoppingCartsSearchResults> results = new ArrayList<>();
        BoundHashOperations<String, Object, Object> boundHashOperations
                = redisTemplate.boundHashOps(personId);
        List<Object> values = boundHashOperations.values();
        values
            .stream()
            .map(x -> JsonUtils.readValue(JsonUtils.toJSONString(x), ShoppingCarts.class)) //转化结构
            .collect(Collectors.groupingBy(ShoppingCarts::getShopId))
            .forEach((key, value) -> results.add(new ShoppingCartsSearchResults(key, value)));
        return results;
    }
}
